package com.wc.算法提高课.E第五章_数学知识.容斥原理.破译密码;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/5 17:36
 * @description https://www.acwing.com/problem/content/217/
 */
public class Main {
    /**
     * 背景：<p>
     * 莫比乌斯函数, x = /sum_1_k p[i] ^ a[i]<p>
     * mobius[x] = {<p>
     * 0, a[i] > 1<p>
     * 1, k为偶数<p>
     * -1, k为奇数<p>
     * }<p>
     * 经典的问题, 求与1 ~ N 与 N 互质的个数? <p>
     * ans = N - S[2] - S[3] - ... + S[2, 3] + S[3,5] - ..., S[i,j] 有单独质因数i, j的个数, S[i,j,k,...]对应的符号就是莫比乌斯函数<p>
     * a / x = a / g(x), g(x) = a / (a / x), g(x) 表示整除时候的最大值, a / x < a / (g(x) + 1) <p>
     * 思路：<p>
     * 题目含义  x <= a, y <= b 存在多少个数对使得gcd(x,y) = d<p>
     * 等价于gcd(x / d, y / d) = 1, x' <= a / d = a', y' <= b / d = b'<p>
     * 等价于 x', y'互质, 很容易想到欧拉函数是吧, 但是不能用, 因为他们的范围是不同的
     * 可以用容斥原理的<p>
     * 其中一共有多少个方案数呢 - 都有公因子的数就是答案， 有一个公因子质数, 有两个公因子质数...<p>
     * a' * b' - S[2] - ... + S[2, 3] + ... + ...
     * S[i, j] = a'/ (i * j) * (b' / (i * j))
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 50010, cnt = 0;
    static int[] mobius = new int[N], primes = new int[N], sum = new int[N];
    static boolean[] st = new boolean[N];

    public static void main(String[] args) {
        ola(N - 1);
        int T = sc.nextInt();
        while (T-- > 0) {
            int a = sc.nextInt(), b = sc.nextInt(), d = sc.nextInt();
            a = a / d;
            b = b / d;
            long res = 0;
            int n = Math.min(a, b);
            for (int l = 1, r = 0; l <= n; l = r + 1) {
                // 跳跃函数, 求g(x) 的过程
                r = Math.min(n, Math.min(a / (a / l), b / (b / l)));
                res += (sum[r] - sum[l - 1]) * (long) (a / l) * (b / l);
            }
            out.println(res);
        }
        out.flush();
    }

    // 线性筛求莫比乌斯函数
    static void ola(int n) {
        st[0] = st[1] = true;
        mobius[1] = 1;
        for (int i = 2; i <= n; i++) {
            if (!st[i]) {
                primes[cnt++] = i;
                mobius[i] = -1;
            }
            for (int j = 0; i * primes[j] <= n; j++) {
                st[i * primes[j]] = true;
                if (i % primes[j] == 0) {
                    // 因为 i % primes[j] = 0, i * primes[j] 至少有 primes[j] ^ 2的质因子
                    mobius[i * primes[j]] = 0;
                    break;
                }
                mobius[i * primes[j]] = mobius[i] * -1;
            }
        }
        for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + mobius[i];
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
